<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('CurrencyModel')) return;
/**
 * EcataloguE
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage ecatalogue
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/EcataloguE
 */

/**
 *  CurrencyModel class 
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com) 
 */
include_spip('classes/pdk_model'); 
include_spip('functions/pdk_common');
class CurrencyModel extends pdk_Model {   
    /**
     *  RubriqueCurrencyModel
     */
    private $rub_cur_mdl;
    
    /**
     *  PdkRubriqueModel
     */
    private $rub_mdl;
    
    /**
     *  PdkArticleModel
     */
    private $art_mdl;
        
    public function __construct() {
        parent::__construct();
        
        $this->config_model();
        
        $this->rub_cur_mdl = pdk_load_class('RubriqueCurrencyModel','classes');        
        $this->rub_mdl = pdk_load_class('PdkRubriqueModel','classes/models');
        $this->art_mdl = pdk_load_class('PdkArticleModel','classes/models');        
    }
    
    function config_model() {
        // Define main table:
        $this->main_table = 'spip_ecatalogue_currencies';
        
        // Define primary key:
        $this->primary_key = 'id_currency';
        
        // Define main table fields:
        $this->main_fields['id_currency'] = 'id_currency'; 
        $this->main_fields['titre'] = 'titre';        
        $this->main_fields['sign_pre'] = 'sign_pre';
        $this->main_fields['sign_after'] = 'sign_after';
        $this->main_fields['code'] = 'code';
        $this->main_fields['rate'] = 'rate';
                                                                                 
         // Define validation rules:
         $this->validation->set_rules( 
            array(
                array(
                     'field'   => 'id_currency',
                     'label'   => 'ID currency',
                     'rules'   => ''
                  ),
                array(
                     'field'   => 'titre',
                     'label'   => _T('ecatalogue:currency_titre'),
                     'rules'   => 'required'
                  ),
                array(
                     'field'   => 'sign_pre',
                     'label'   => _T('ecatalogue:currency_sign_pre'),
                     'rules'   => ''
                  ),    
                array(
                     'field'   => 'sign_after',
                     'label'   => _T('ecatalogue:currency_sign_after'),
                     'rules'   => ''
                  ),  
                array(
                     'field'   => 'code',
                     'label'   => _T('ecatalogue:currency_code'),
                     'rules'   => 'required'
                  ),  
                array(
                     'field'   => 'rate',
                     'label'   => _T('ecatalogue:currency_rate'),
                     'rules'   => 'required'
                  ),  
            )
         );                                       
    }
    
    // Define test data
    public $test_data = array(
        array('id_currency'=>1,'titre'=>'<multi>[ru]Доллар США[en]U.S. Dollar</multi>', 'sign_pre'=>'$', 'sign_after'=>'', 'code'=>'USD', 'rate'=>'0.125'),
        array('id_currency'=>2,'titre'=>'<multi>[ru]Украинская гривна[en]Ukrainian hryvnia</multi>', 'sign_pre'=>'', 'sign_after'=>'hrn','code'=>'UAH', 'rate'=>'1'),
        array('id_currency'=>3,'titre'=>'<multi>[ru]Євро[en]Euro</multi>', 'sign_pre'=>'€', 'sign_after'=>'', 'code'=>'EUR', 'rate'=>'0.0958'),
    );
    
    public function SelectByCode($code) {
        $currency = $this->select("code='$code'");
        if ($currency)
            return $currency[0];
        else
            return false;     
    } 
    
    /**
     *  Finds currency for given rubrique
     *  This method takes to account currency inheritance possibility
     *  @access public
     *  @param int $id_rubrique ID rubrique
     *  @return Currency array if currency for given rubrique exists or false otherwise
     */
    public function GetCurrencyByIdRubrique($id_rubrique) {        
        // Get link for given rubrique
        // If link exists, return currency
        $rub_cur = $this->rub_cur_mdl->select('id_rubrique='.$id_rubrique);        
        if ($rub_cur) {
            $rub_cur = $rub_cur[0];
            return $this->select_by_id($rub_cur['id_currency']);
        }
                
        $rubrique = $this->rub_mdl->select_by_id($id_rubrique);
        // Get link for parent rubrique
        $rub_cur = $this->rub_cur_mdl->select('id_rubrique='.$rubrique['id_parent']);
        // If link for parent rubrique exists, return currency       
        if ($rub_cur) {
            $rub_cur = $rub_cur[0];
            return $this->select_by_id($rub_cur['id_currency']);
        }
        // If our rubrique has parent, make a recursive call
        if ($rubrique['id_parent'] != 0)
            return $this->GetCurrencyByIdRubrique($rubrique['id_parent']);
        
        return false;
    }
    
    /**
     *  Finds currency for given article
     *  This method takes to account currency inheritance possibility
     *  @access public
     *  @param int $id_article ID article
     *  @return Currency array if currency for given rubrique exists or false otherwise
     */
    public function GetCurrencyByIdArticle($id_article) {
        $article = $this->art_mdl->select_by_id($id_article);
        
        if (!$article['id_rubrique']) return false;
        
        return $this->GetCurrencyByIdRubrique($article['id_rubrique']);
    }
    
    /**
     *  Gets currency sign by id article
     *  @param int $id_article ID article
     *  @param string $sign Sign field (sign_pre or sign_after). Default: sign_pre
     */
    public function GetCurrencySignByIdArticle($id_article, $sign='sign_pre') {
        $currency = $this->GetCurrencyByIdArticle($id_article);
        if (!$currency) return false;
        
        if (!isset($currency[$sign]))
            throw new Exception("Currency field \"$sign\" doesn't exist!");
        
        return $currency[$sign];   
    }
} 
?>